"""
  Copyright 2008 Texas A&M University Licensed under the
  Educational Community License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may
  obtain a copy of the License at
  
  http://www.osedu.org/licenses/ECL-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an "AS IS"
  BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  or implied. See the License for the specific language governing
  permissions and limitations under the License.
"""

from os import path
import unittest
from StringIO import StringIO
from lxml import etree

from meercat.base import Metadata
from meercat.mapreduce.mappers import xsl



class BaseTest(unittest.TestCase):
    source = None
    stylesheet = None
    output = None
    
    def setUp(self):
        pass
    
    def testSimple(self):
        test_output = self.stylesheet(self.source)
        
        self.assertEqual(etree.tostring(test_output, encoding='utf-8',pretty_print=False),
                         etree.tostring(etree.parse(self.output), encoding='utf-8',
                                        pretty_print=False))

"""    
class MarcXMLTest(BaseTest):
    source = etree.parse(path.join(path.dirname(xsl.__file__),
                                   'tests','data','marc.xml'))
    stylesheet = etree.XSLT(etree.parse(path.join(path.dirname(xsl.__file__),
                                                  'xslt',
                                                  'MARC21slim2SOLR.xsl')))
    output = path.join(path.dirname(xsl.__file__),
                       'tests','data','marc.solr.xml')
"""

class SFXTest(BaseTest):
    source = etree.parse(path.join(path.dirname(xsl.__file__),
                                   'tests','data','sfx.xml'))
    stylesheet = etree.XSLT(etree.parse(path.join(path.dirname(xsl.__file__),
                                                  'xslt',
                                                  'Sfx3toSOLR.xsl')))
    output = path.join(path.dirname(xsl.__file__),
                       'tests','data','sfx.solr.xml')

class MetalibCategoryTest(BaseTest):
    source = etree.parse(path.join(path.dirname(xsl.__file__),
                                   'tests','data','metalib.category.xml'))
    stylesheet = etree.XSLT(etree.parse(path.join(path.dirname(xsl.__file__),
                                                  'xslt',
                                                  'MetalibCategory2SOLR.xsl')))
    output = path.join(path.dirname(xsl.__file__),
                       'tests','data','metalib.category.solr.xml')

class MetalibMarcTest(BaseTest):
    source = etree.parse(path.join(path.dirname(xsl.__file__),
                                   'tests','data','metalib.marc.xml'))
    stylesheet = etree.XSLT(etree.parse(path.join(path.dirname(xsl.__file__),
                                                  'xslt',
                                                  'MetalibMARC2SOLR.xsl')))
    output = path.join(path.dirname(xsl.__file__),
                       'tests','data','metalib.marc.solr.xml')

class VoyagerTest(BaseTest):
    source = etree.parse(path.join(path.dirname(xsl.__file__),
                                   'tests','data','voyager.xml'))
    stylesheet = etree.XSLT(etree.parse(path.join(path.dirname(xsl.__file__),
                                                  'xslt',
                                                  'Voyager2SOLR.xsl')))
    output = path.join(path.dirname(xsl.__file__),
                       'tests','data','voyager.solr.xml')

def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SFXTest))
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(MetalibCategoryTest))
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(MetalibMarcTest))
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(VoyagerTest))
    return suite

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(test_suite())